package com.xuxueli.applyModules.commont;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 执行任务逻辑
 */
@Slf4j
public class TaskExecuteJob extends QuartzJobBean {


    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {
        Long taskId = jobExecutionContext.getJobDetail().getJobDataMap().getLong("id");
        // 判断是否触发执行
        boolean validExecuteTask = isValidExecuteTask(taskId);
        if (validExecuteTask) {
            log.info("定时任务开始: {}", LocalDateTime.now());
            // 获得任务创建执行者，和产品确认过若自动执行为任务的创建者为job的创建执行者
            String loginName = "test";
            log.info("定时任务结束");
        }

    }

    /**
     * 判断该任务是否有效、是否过期
     *
     * @param taskId
     * @return
     */
    public boolean isValidExecuteTask(Long taskId) {
        TaskBasicDTO taskBasicDTO = new TaskBasicDTO();
        taskBasicDTO.setTimeBegin(new Date().getTime());
        taskBasicDTO.setTimeEnd(new Date().getTime());
        if (taskBasicDTO == null) {
            return false;
        }
        Long timeBegin = taskBasicDTO.getTimeBegin();
        Long timeEnd = taskBasicDTO.getTimeEnd();
        if (timeBegin != null && timeEnd != null && System.currentTimeMillis() <= timeBegin
            && System.currentTimeMillis() >= timeEnd) {
            return false;
        }
        return true;
    }

}
